package com.yxt.common.ui;

import java.util.List;

import com.yxt.ui.R;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NormalTableAdapter extends BaseAdapter {
	private Context context;
	private List<TableRow> table;

	public NormalTableAdapter(Context context, List<TableRow> table) {
		this.context = context;
		this.table = table;
	}


	public int getCount() {
		return table.size();
	}

	public long getItemId(int position) {
		return position;
	}

	public TableRow getItem(int position) {
		return table.get(position);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		TableRow tableRow = table.get(position);
		return new TableRowView(this.context, tableRow);
	}

	/**
	 * TableRowView 实现表格行的样式
	 * 
	 * @author hellogv
	 */
	class TableRowView extends LinearLayout {
		public TableRowView(Context context, TableRow tableRow) {
			super(context);
			this.setBackgroundResource(R.drawable.list_selector);
			this.setOrientation(LinearLayout.HORIZONTAL);
			for (int i = 0; i < tableRow.getSize(); i++) {// 逐个格单元添加到行
				TableCell tabcell = tableRow.getCellValue(i);
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						tabcell.width, tabcell.height);// 按照格单元指定的大小设置空间
				if (tabcell.type == TableCell.STRING) {// 如果格单元是文本内容
					TextView cell = new TextView(context);
					cell.setText(String.valueOf(tabcell.value));
					cell.setTextSize(15);
					cell.setTextColor(Color.BLACK);
					cell.setGravity(Gravity.CENTER_VERTICAL);
					cell.setEllipsize(TruncateAt.END);
					cell.setSingleLine(true);
					if (tabcell.listener != null) {
						cell.setOnClickListener(tabcell.listener);
					}
					layoutParams.weight = 1;
					layoutParams.setMargins(0, 0, 0, 0);// 预留空隙制造边框
					addView(cell, layoutParams);
				} else if (tabcell.type == TableCell.DISABLE) {// 如果格单元是文本内容
					TextView cell = new TextView(context);
					cell.setText(String.valueOf(tabcell.value));
					cell.setTextSize(15);
					cell.setTextColor(Color.WHITE);
					cell.setGravity(Gravity.CENTER_VERTICAL);
					cell.setEllipsize(TruncateAt.END);
					cell.setSingleLine(true);
					cell.setClickable(false);
					cell.setBackgroundColor(Color.GRAY);
					if (tabcell.listener != null) {
						cell.setOnClickListener(tabcell.listener);
					}
					layoutParams.weight = 1;
					layoutParams.setMargins(0, 0, 0, 0);// 预留空隙制造边框
					addView(cell, layoutParams);
				} else if (tabcell.type == TableCell.DEFINE) {// 如果格单元是文本内容
					LinearLayout layout = new LinearLayout(context);
					layout.setOrientation(VERTICAL);
					String[] values = String.valueOf(tabcell.value).split("@");
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, tabcell.height / 2 );
					TextView top = new TextView(context);
					top.setText(values[0]);
					top.setTextSize(18);
					top.setTextColor(Color.BLACK);
					top.setGravity(Gravity.CENTER_VERTICAL);
					top.setEllipsize(TruncateAt.END);
					top.setSingleLine(true);
					layout.addView(top,params);
					
					TextView bottom = new TextView(context);
					String addr =("".equals(values[1]) ? "" : values[1]);
					bottom.setText(addr);
					bottom.setTextSize(15);
					bottom.setGravity(Gravity.CENTER_VERTICAL);
					bottom.setEllipsize(TruncateAt.END);
					bottom.setSingleLine(true);
					layout.addView(bottom,params);
					
					layoutParams.weight = 1;
					layoutParams.setMargins(5, 0, 0, 0);// 预留空隙制造边框
					if (tabcell.listener != null) {
						layout.setOnClickListener(tabcell.listener);
					}
					addView(layout, layoutParams);
				} else if (tabcell.type == TableCell.IMAGE) {// 如果格单元是图像内容
					ImageView image = new ImageView(context);
					image.setImageResource(Integer.parseInt(String
							.valueOf(tabcell.value)));
					if (tabcell.listener != null) {
						image.setOnClickListener(tabcell.listener);
					}
					layoutParams.width = 25;
					layoutParams.height = 25;
					layoutParams.gravity = Gravity.CENTER;
					addView(image, layoutParams);

				}
			}
		}
	}

	/**
	 * TableRow 实现表格的行
	 * 
	 * @author hellogv
	 */
	static public class TableRow {
		private TableCell[] cell;

		public TableRow(TableCell[] cell) {
			this.cell = cell;
		}

		public int getSize() {
			return cell.length;
		}

		public TableCell getCellValue(int index) {
			if (index >= cell.length)
				return null;
			return cell[index];
		}
	}

	/**
	 * TableCell 实现表格的格单元
	 * 
	 * @author hellogv
	 */
	static public class TableCell {
		static public final int STRING = 0;
		static public final int IMAGE = 1;
		static public final int DEFINE = 2;
		static public final int DISABLE = 3;

		public Object value;
		public int width;
		public int height;
		private int type;
		public int color;
		public OnClickListener listener;

		public TableCell(Object value, int width, int height, int color,int type, OnClickListener listener) {
			this.value = value;
			this.width = width;
			this.height = height;
			this.type = type;
			this.color = color;
			this.listener = listener;
		}

		public TableCell(Object value, int width, int height, int color,int type) {
			this(value, width, height, color, type, null);
		}
	}
}